package com.aldobucchi.x.flash.display
{
	import com.aldobucchi.core.markers.IIncomplete;
	import com.aldobucchi.x.mx.collections.IList.IListUtil;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	

	/**
	 * 
	 * This class allows you to wrap a flash.display.DisplayObjectContainer to expose
	 * its children as a standard IList.
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class DisplayObjectContainerChildrenList extends ArrayCollection implements IIncomplete
	{

		private var container:DisplayObjectContainer;
		
		private var pollingTimer:Timer;
		
		private var passThruToSuperFlag:Boolean = false;
		
		public function DisplayObjectContainerChildrenList( container:DisplayObjectContainer, pollingInterval:int = 500 )
		{
			this.container = container;
		
			// initial copy
			passThruToSuperFlag = true;
			
			for ( var i:int=0; i<container.numChildren; i++ )
				super.addItem( container.getChildAt( i ) );			
			
			passThruToSuperFlag = false;


			// start the polling timer
			pollingTimer = new Timer( pollingInterval );
			pollingTimer.addEventListener( TimerEvent.TIMER, onTimer );
			pollingTimer.start();
		}

		
		private function onTimer( event:TimerEvent ):void
		{
			passThruToSuperFlag = true;
			super.disableAutoUpdate();
			
			IListUtil.equalizeListToArrayKeepingOrder( this, DisplayObjectContainerUtil.getDescendants( container, false, false ) );

			super.enableAutoUpdate();
			passThruToSuperFlag = false;
		}


		/////////// editing ///////////
		
		// we need to use a flag to indicate wether the following methods should pass thru or not
		
		override public function removeAll():void
		{
			if ( passThruToSuper )
			{
				super.removeAll();
				return;
			}
				
			trace( "removeAll" );
			DisplayObjectContainerUtil.removeAllChildren( container );			
		}
		
		override public function addItem( item:Object ):void
		{
			if ( passThruToSuper )
			{
				super.addItem( item );
				return;
			}
			
			trace("addItem");
			container.addChild( item as DisplayObject );
		}
		
		override public function addItemAt(item:Object, index:int):void
		{
			if ( passThruToSuper )
			{
				super.addItemAt( item, index );
				return;
			}
						
			trace("addItemAt");
			container.addChildAt( item as DisplayObject, index );
		}
		
		override public function setItemAt( item:Object, index:int ):Object
		{
			if ( passThruToSuper )
				return 	super.setItemAt( item, index );		
			
			trace("setItemAt");
			return container.addChildAt( item as DisplayObject, index );		
		}

		override public function removeItemAt( index:int ):Object
		{
			if ( passThruToSuper )
				return super.removeItemAt( index );
			
			trace("removeItemAt");
			return container.removeChildAt( index );
		}
		
		
		
		
		
		
		
		private function get passThruToSuper():Boolean
		{
			return passThruToSuperFlag;
		}
		

		
		// we disable some methods that have no sense
		// and would result in unpredictable behaviour
		
		override public function enableAutoUpdate():void{}
	
		override public function disableAutoUpdate():void{}	

	}

}